package com.itheima.protocol.rmiDemo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class User_Skeleton extends Thread {

    private UserServer userServer;

    public User_Skeleton(UserServer userServer) {
        this.userServer = userServer;
    }

    @Override
    public void run() {

        ServerSocket serverSocket = null;
        ObjectInputStream read = null;
        ObjectOutputStream write = null;
        try {
            serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();

            while (socket != null) {

                read = new ObjectInputStream(socket.getInputStream());
                String method = (String) read.readObject();

                if(method.equals("age")){
                    int age = userServer.getAge();
                    
                    //传送到客户端
                    write = new ObjectOutputStream(socket.getOutputStream());
                    write.writeInt(age);
                    write.flush();
                }
            }

        } catch (IOException e) {

            e.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){

                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(read !=null){
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(write != null){
                try {
                    write.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }



        }

    }
}
